package com.app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.app.entity.User;
import com.app.entity.UserHome;
import com.app.exception.ValidateException;
import com.app.requestVO.UserListRequestVO;
import com.app.responseVO.ReturnResponseVO;
import com.app.responseVO.ReturnResultResponseVO;
import com.app.responseVO.UserResponseVO;
import com.app.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.ValidationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xinjian.hu on 18-3-20.
 */
@Controller
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "user/doLogin", method = RequestMethod.POST)
    public void doLogin(@RequestBody String body,
                        HttpServletResponse response,HttpServletRequest request) throws IOException {
        User user = JSON.parseObject(body, User.class);
        UserResponseVO userResponseVO = new UserResponseVO();
        try {
            boolean symbol = userService.doLogin(user);
            userResponseVO.setStatue(symbol);
            if (symbol) {
                userResponseVO.setMsg("登录成功");
                request.getSession().setAttribute("userLogin", user.getUsername());
            } else {
                userResponseVO.setMsg("登录失败");
            }
        } catch (ValidateException e) {
            userResponseVO.setMsg(e.getMsg());
            userResponseVO.setStatue(false);
        }
        writeJSON(response, userResponseVO);
    }
    /**
     * admin 登录
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "user/doAdminLogin", method = RequestMethod.POST)
    public void doAdminLogin(@RequestBody String body,
                        HttpServletResponse response) throws IOException {
        User user = JSON.parseObject(body, User.class);
        UserResponseVO userResponseVO = new UserResponseVO();
        try {
            boolean symbol = userService.doAdminLogin(user);
            userResponseVO.setStatue(symbol);
            if (symbol) {
                userResponseVO.setMsg("登录成功");
            } else {
                userResponseVO.setMsg("登录失败");
            }
        } catch (ValidateException e) {
            userResponseVO.setMsg(e.getMsg());
            userResponseVO.setStatue(false);
        }
        writeJSON(response, userResponseVO);
    }

    /**
     * 分页获取用户列表
     *
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping("user/queryUserList")
    public void queryUserList(@RequestBody String body,
                              HttpServletResponse response) throws IOException {
        ReturnResultResponseVO<User> returnResultResponseVO = new ReturnResultResponseVO<User>();
        try {
            //PageVO pageVO = new PageVO();
            UserListRequestVO userListRequestVO = JSON.parseObject(body, UserListRequestVO.class);
            if(null == userListRequestVO || null == userListRequestVO.getPageVO()){
                throw new ValidationException("参数格式有误");
            }
            returnResultResponseVO = userService.getUserList(userListRequestVO.getPageVO().getCurrentPage(), userListRequestVO.getPageVO().getPagesize(), userListRequestVO.getUserName());
            returnResultResponseVO.setStatue(true);
        } catch (ValidationException e) {
            returnResultResponseVO.setStatue(false);
        }
        writeJSON(response, returnResultResponseVO);
    }

    /**
     * 获取用户详情
     *
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping("user/getUserHomeDetail")
    public void getUserHomeDetail(@RequestBody String body,
                                  HttpServletResponse response) throws IOException {
        User user = JSON.parseObject(body, User.class);
        UserHome userHome = userService.getUserHome(user.getUsername());
        ReturnResponseVO<UserHome> returnResponseVO = new ReturnResponseVO<UserHome>();
        returnResponseVO.setData(userHome);
        returnResponseVO.setStatue(true);
        returnResponseVO.setMsg("成功");
        writeJSON(response, returnResponseVO);
    }
    @RequestMapping("user/getUserHomeDetailPageList")
    @ResponseBody
    public JSONObject getUserHomeDetailPageList(@RequestParam("page") int currentPage,@RequestParam("limit") int pagesize,
			HttpServletResponse response,HttpServletRequest request) throws IOException {
    	JSONObject jsonObject = new JSONObject();  
    	UserHome userHome = userService.getUserHome((String)request.getSession().getAttribute("userLogin"));
    	List<UserHome> users = new ArrayList<UserHome>();
    	users.add(userHome);
    	jsonObject.put("count", 1);  
		jsonObject.put("data", users); 
		jsonObject.put("code", 0);  
		jsonObject.put("msg", "");
    	//writeJSON(response, returnResponseVO);
    	return jsonObject;
    }
    @RequestMapping("user/updateUserFee")
    @ResponseBody
    public Map<String,Object> updateUserFee(String userHomeId){
    	Map<String,Object> map = new HashMap<String, Object>();
    	Map<String, Object> oParam = new HashMap<String, Object>();
    	oParam.put("fee", 1);
    	oParam.put("userHomeId", userHomeId);
    	boolean flag = userService.updateUserFee(oParam);
    	map.put("success", flag);
    	map.put("msg", "");
    	
    	return map;
    }
    /**
     * 新增用户
     *
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "user/addUser", method = RequestMethod.POST)
    public void addUser(@RequestBody String body,
                        HttpServletResponse response) throws IOException {
        UserResponseVO userResponseVO = new UserResponseVO();
        User user = JSON.parseObject(body, User.class);
        try {
            userService.addUser(user.getUsername(), user.getPassword());
            userResponseVO.setMsg("成功");
            userResponseVO.setStatue(true);
        } catch (ValidateException e) {
            userResponseVO.setMsg(e.getMsg());
            userResponseVO.setStatue(false);
        }
        writeJSON(response, userResponseVO);
    }

    /**
     * 修改用户状态
     *
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "user/updateUser")
    public void updateUser(@RequestBody String body,
                           HttpServletResponse response) throws IOException {
        UserResponseVO userResponseVO = new UserResponseVO();
        User user = JSON.parseObject(body, User.class);
        try {
            userService.updateStatus(user.getUsername(), user.getUserState());
            userResponseVO.setStatue(true);
            userResponseVO.setMsg("成功");
        } catch (ValidateException e) {
            userResponseVO.setStatue(false);
            userResponseVO.setMsg("失败");
        }
        writeJSON(response, userResponseVO);
    }

    /**
     * 删除用户
     *
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "user/deleteUser")
    public void deleteUser(@RequestBody String body,
                           HttpServletResponse response) throws IOException {
        UserResponseVO userResponseVO = new UserResponseVO();
        User user = JSON.parseObject(body, User.class);
        try {
            userService.deleteUser(user.getUsername());
            userResponseVO.setStatue(true);
            userResponseVO.setMsg("成功");
        } catch (ValidateException e) {
            userResponseVO.setStatue(false);
            userResponseVO.setMsg("失败");
        }
        writeJSON(response, userResponseVO);
    }

    /**
     * 更新用户信息
     *
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "user/updateUserHome", method = RequestMethod.POST)
    public void updateUserHome(@RequestBody String body,
                               HttpServletResponse response) throws IOException {
        UserResponseVO userResponseVO = new UserResponseVO();
        UserHome userHome = JSON.parseObject(body, UserHome.class);
        try {
            userService.updateUserHome(userHome);
            userResponseVO.setStatue(true);
            userResponseVO.setMsg("成功");
        } catch (ValidateException e) {
            userResponseVO.setStatue(false);
            userResponseVO.setMsg("失败");
        }
        writeJSON(response, userResponseVO);
    }
}
